package com.qifenqian.turbot.module.model

import android.content.Context
import com.qifenqian.turbot.base.BaseModel
import com.qifenqian.turbot.module.contract.AccountContract
import com.qifenqian.turbot.module.entity.response.LoginEntity
import com.qifenqian.turbot.module.entity.response.RegisterEntity
import com.qifenqian.turbot.module.entity.response.UploadEntity
import com.qifenqian.turbot.module.entity.response.VersionEntity
import com.qifenqian.turbot.net.ModelCallback
import com.qifenqian.turbot.net.NetObserver

/**
 * @author H
 * @createTime 2018/12/28 17:47
 * @description
 * @see
 * @since
 */
class AccountModel(context: Context) : BaseModel(context), AccountContract.Model {

    override fun uploadFile(filePath: String, callback: ModelCallback<UploadEntity>) {
        mClient!!.uploadFile(filePath).subscribe(object : NetObserver<UploadEntity>(mContext, true) {
            override fun onSuccess(msg: String?, t: UploadEntity?) {
                callback.onSuccess(msg, t)
            }

            override fun onFailure(code: Int, msg: String?) {
                callback.onFailure(code, msg)
            }
        })
    }

    override fun logout(userCode: String, callback: ModelCallback<Nothing>) {
        mClient!!.logout(userCode)
                .subscribe(object : NetObserver<Nothing>(mContext, false) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun giveFeedback(content: String, callback: ModelCallback<Nothing>) {
        mClient!!.giveFeedback(content)
                .subscribe(object : NetObserver<Nothing>(mContext, false) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun modifyPassword(oldPassword: String, newPassword: String, callback: ModelCallback<Nothing>) {
        mClient!!.modifyPassword(oldPassword, newPassword)
                .subscribe(object : NetObserver<Nothing>(mContext, false) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun modifyPhone(oldPhone: String, password: String, newPhone: String, smsCode: String, callback: ModelCallback<Nothing>) {
        mClient!!.modifyPhone(oldPhone, password, newPhone, smsCode)
                .subscribe(object : NetObserver<Nothing>(mContext, false) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }

                })
    }

    override fun checkVersion(callback: ModelCallback<VersionEntity>?) {
        mClient!!.checkVersion()
                .subscribe(object : NetObserver<VersionEntity>(mContext, false) {
                    override fun onSuccess(msg: String?, t: VersionEntity?) {
                        callback!!.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback!!.onFailure(code, msg)
                    }
                })
    }

    override fun sendSmsCode(type: Int, callback: ModelCallback<Nothing>) {
        mClient!!.sendSmsCode(type)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun login(userCode: String, password: String, loginType: Int, smsCode: String, showDialog: Boolean, callback: ModelCallback<LoginEntity>) {
        mClient!!.login(userCode, password, loginType, smsCode)
                .subscribe(object : NetObserver<LoginEntity>(mContext, showDialog) {
                    override fun onSuccess(msg: String?, t: LoginEntity?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun register(userCode: String, smsCode: String, password: String, callback: ModelCallback<RegisterEntity>) {
        mClient!!.register(userCode, smsCode, password)
                .subscribe(object : NetObserver<RegisterEntity>(mContext, true) {
                    override fun onSuccess(msg: String?, t: RegisterEntity?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun identityVerifyFirst(userCode: String, frontPhotoOfIdCard: String, backPhotoOfIdCard: String, bankNo: String, bankName: String, phone: String, smsCode: String, callback: ModelCallback<Nothing>) {
        mClient!!.qualityCertificationFirst(userCode, frontPhotoOfIdCard, backPhotoOfIdCard, bankNo, bankName, phone, smsCode)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun identityVerifySecond(userCode: String, businessLicense: String, storePhotos: MutableList<String>, cateringServiceLicense: String, callback: ModelCallback<Nothing>) {
        mClient!!.qualityCertificationSecond(userCode, businessLicense, storePhotos, cateringServiceLicense)
                .subscribe(object : NetObserver<Nothing>(mContext, true) {
                    override fun onSuccess(msg: String?, t: Nothing?) {
                        callback.onSuccess(msg, t)
                    }

                    override fun onFailure(code: Int, msg: String?) {
                        callback.onFailure(code, msg)
                    }
                })
    }

    override fun resetPassword(userCode: String, password: String, smsCode: String, callback: ModelCallback<*>) {
        mClient!!.resetPassword(userCode, password, smsCode).subscribe(object : NetObserver<Nothing>(mContext, true) {
            override fun onSuccess(msg: String?, t: Nothing?) {
                callback.onSuccess(msg, t)
            }

            override fun onFailure(code: Int, msg: String?) {
                callback.onFailure(code, msg)
            }
        })
    }
}
